package ro.ss.bb.deployer.ui.start;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import ro.ss.bb.deployer.invoker.AntInvoker;

/**
 * Replace with your class description.
 */
public class DeployerUI extends JFrame implements ActionListener {

    /** here add the new server name */
    private String[] servers = { "tomcat", "jboss" ,"weblogic", "oas"};

    /** here implement the new server needs */
    private void initAuxPanel(String serverName) {

        if(inputs!=null && inputs.size()>0){
            inputs.clear();
        }else{
            inputs = new ArrayList<JTextField>();
        }
        if(panel0bis.getComponentCount()>0){
            panel0bis.removeAll();
            panel0bis.revalidate();
            panel0bis.repaint();
        }
        
        // panel0bis holds the controllers:
        if (servers[0].equals(serverName)) { //tomcat
            JTextField tomcatServer = new JTextField("http://localhost:8080/");
            tomcatServer.setName("tomcat.server");
            inputs.add(tomcatServer);
            JTextField managerUrl = new JTextField("manager");
            managerUrl.setName("tomcat.manager.url");
            inputs.add(managerUrl);
            JTextField user = new JTextField("admin");
            user.setName("tomcat.username");
            inputs.add(user);
            JTextField pass = new JTextField("admin");
            pass.setName("tomcat.password");
            inputs.add(pass);
            JTextField webAppName = new JTextField("myApp");
            webAppName.setName("webapp.name");
            inputs.add(webAppName);

            panel0bis.add(new JLabel("Tomcat server address:"));
            panel0bis.add(tomcatServer);
            panel0bis.add(new JLabel("Manager url:"));
            panel0bis.add(managerUrl);
            panel0bis.add(new JLabel("Admin username:"));
            panel0bis.add(user);
            panel0bis.add(new JLabel("Admin password:"));
            panel0bis.add(pass);
            panel0bis.add(new JLabel("Application name:"));
            panel0bis.add(webAppName);
        } else if (servers[1].equals(serverName)) { //jboss
            JTextField server = new JTextField("http://localhost:8080/");
            server.setName("server");
            inputs.add(server);
            JTextField managerUrl = new JTextField("manager");
            managerUrl.setName("manager.url");
            inputs.add(managerUrl);
            JTextField user = new JTextField("admin");
            user.setName("username");
            inputs.add(user);
            JTextField pass = new JTextField("admin");
            pass.setName("password");
            inputs.add(pass);
            JTextField webAppName = new JTextField("myApp");
            webAppName.setName("webapp.name");
            inputs.add(webAppName);


            panel0bis.add(new JLabel("Server address:"));
            panel0bis.add(server);
            panel0bis.add(new JLabel("Manager url:"));
            panel0bis.add(managerUrl);
            panel0bis.add(new JLabel("Admin username:"));
            panel0bis.add(user);
            panel0bis.add(new JLabel("Admin password:"));
            panel0bis.add(pass);
            panel0bis.add(new JLabel("Application name:"));
            panel0bis.add(webAppName);
        } else if (servers[2].equals(serverName)) { //weblogic
            JTextField server = new JTextField("t3://localhost:7001/");
            server.setName("server");
            inputs.add(server);
            JTextField user = new JTextField("admin");
            user.setName("username");
            inputs.add(user);
            JTextField pass = new JTextField("admin");
            pass.setName("password");
            inputs.add(pass);
            JTextField webAppName = new JTextField("myApp");
            webAppName.setName("webapp.name");
            inputs.add(webAppName);
            JTextField domain = new JTextField("myDomain");
            domain.setName("domain");
            inputs.add(domain);
            JTextField classPath = new JTextField("\\\\localhost\\c\\OracleBEA\\wlserver_10.3\\server\\lib\\weblogic.jar");
            classPath.setName("class.path");
            inputs.add(classPath);

            panel0bis.add(new JLabel("Tomcat server address:"));
            panel0bis.add(server);
            panel0bis.add(new JLabel("Admin username:"));
            panel0bis.add(user);
            panel0bis.add(new JLabel("Admin password:"));
            panel0bis.add(pass);
            panel0bis.add(new JLabel("Application name:"));
            panel0bis.add(webAppName);
            panel0bis.add(new JLabel("Domain:"));
            panel0bis.add(domain);
            panel0bis.add(new JLabel("ClassPath (where is weblogic.jar):"));
            panel0bis.add(classPath);
        } else if (servers[3].equals(serverName)) { //oas
            JTextField server = new JTextField("deployer:oc4j:localhost:23791");
            server.setName("deployer.uri");
            inputs.add(server);
            JTextField user = new JTextField("admin");
            user.setName("oc4j.admin.user");
            inputs.add(user);
            JTextField pass = new JTextField("pass");
            pass.setName("oc4j.admin.password");
            inputs.add(pass);
            JTextField webAppName = new JTextField("myApp");
            webAppName.setName("webapp.name");
            inputs.add(webAppName);

            panel0bis.add(new JLabel("Deployer address:"));
            panel0bis.add(server);
            panel0bis.add(new JLabel("Admin username:"));
            panel0bis.add(user);
            panel0bis.add(new JLabel("Admin password:"));
            panel0bis.add(pass);
            panel0bis.add(new JLabel("Application name:"));
            panel0bis.add(webAppName);
        }
        panel0bis.revalidate();
        panel0bis.repaint();
        
    }

    public DeployerUI() {
        initRadioButtons();
        initAuxPanel(servers[0]);
        selectedServer = servers[0];
        initInterface();
    }

    private void initRadioButtons() {
        // Create the radio buttons.
        ButtonGroup group = new ButtonGroup();
        for (int i = 0; i < radio.length; i++) {
            radio[i] = new JRadioButton(servers[i]);
            radio[i].setName(servers[i]);
            if (i == 0) {
                radio[i].setSelected(true);
            }
            group.add(radio[i]);
            radio[i].addActionListener(this);
            panel0.add(radio[i]);
        }
    }

    protected void initInterface() {
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setTitle("Universal Deployer made easy");

        JPanel panel2 = new JPanel();
        next.addActionListener(this);
        next.setName(NEXT);
        back.addActionListener(this);
        back.setName(BACK);
        back.setEnabled(false);
        finish.setName(FINISH);
        finish.addActionListener(this);
        panel2.add(back);
        panel2.add(next);
        panel2.add(finish);
        panel2.add(step);

        panel1.add(text);
        initPage(0);
        step.setText("Pasul " + (wizzardPage + 1) + " din " + numberOfPages);
        if(numberOfPages<2){
            next.setEnabled(false);
        }

        this.getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
        this.getContentPane().add(panel0);
        this.getContentPane().add(panel0bis);
        this.getContentPane().add(panel1);
        this.getContentPane().add(panel2);
        this.pack();
        this.setBounds(50, 50, 900, 800);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent e) {

        // buttons!
        if (e.getSource() instanceof JButton) {
            JButton myButton = (JButton) e.getSource();
            if (NEXT.equals(myButton.getName())) {
                clearPage(wizzardPage);
                wizzardPage++;
                initPage(wizzardPage);

                if (wizzardPage == numberOfPages - 1) {
                    next.setEnabled(false);
                }
                back.setEnabled(true);
                step.setText("Pasul " + (wizzardPage + 1) + " din " + numberOfPages);
            }
            if (BACK.equals(myButton.getName())) {
                clearPage(wizzardPage);
                wizzardPage--;
                initPage(wizzardPage);

                if (wizzardPage == 0) {
                    back.setEnabled(false);
                }
                next.setEnabled(true);
                step.setText("Pasul " + (wizzardPage + 1) + " din " + numberOfPages);
            }
            if (FINISH.equals(myButton.getName())) {
                clearPage(wizzardPage);
                back.setEnabled(false);
                next.setEnabled(false);
                step.setText("Deploying..^%!$@");
                finish.setEnabled(false);
                setParametersForDeploy();
                startBuildandDeploy();
            }
        } else {
            if (e.getSource() instanceof JRadioButton) {
                JRadioButton myButton = (JRadioButton) e.getSource();
                selectedServer = myButton.getName();
                initAuxPanel(selectedServer);
            }
        }
    }
   
    /**
     * 
     */
    private void setParametersForDeploy() {
        parameters = new HashMap<String, String>();
        for (JTextField input : inputs) {
            parameters.put(input.getName(),input.getText().trim());
        }
    }

    private void startBuildandDeploy() {
        //build
        AntInvoker ai = new AntInvoker();
        ai.getP().executeTarget(AntInvoker.TARGET_BUILD_WAR);
        //deploy
        ai = new AntInvoker(selectedServer, parameters);
        ai.getP().executeTarget(ai.getP().getDefaultTarget());
    }

    private void initPage(int pageNr) {
        text.setText(fileContent);
    }

    private void clearPage(int pageNr) {
        fileContent = text.getText();
    }

    public static void main(String[] args) {
        new DeployerUI();
    }

    private List<JTextField> inputs;
    private Map<String, String> parameters;
    
    private JPanel panel0 = new JPanel();
    private JPanel panel0bis = new JPanel();

    private JRadioButton[] radio = new JRadioButton[servers.length];
    private String selectedServer = "";

    int wizzardPage = 0;
    JPanel panel1 = new JPanel();
    private JButton back = new JButton(BACK);
    private JButton next = new JButton(NEXT);
    private JButton finish = new JButton(FINISH);
    private JLabel step = new JLabel();
    private JTextArea text = new JTextArea();

    private static final String NEXT = "next";
    private static final String BACK = "back";
    private static final String FINISH = "finish";

    public int numberOfPages;

    public String fileContent = null;
}
